package com.ruoyi.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayFundTransUniTransferModel;
import com.alipay.api.domain.Participant;
import com.alipay.api.request.AlipayFundTransUniTransferRequest;
import com.alipay.api.response.AlipayFundTransUniTransferResponse;
import com.ruoyi.config.AlipayConfig;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/transfer")
public class TransferAccountsController {

    @PostMapping("/par")
    public void par() throws AlipayApiException {
        String privateKey = AlipayConfig.APP_PRIVATE_KEY;
        CertAlipayRequest alipayConfig = new CertAlipayRequest();
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setServerUrl("https://openapi.alipaydev.com/gateway.do");
        alipayConfig.setAppId(AlipayConfig.APP_ID);
        alipayConfig.setCharset("UTF8");
        alipayConfig.setSignType("RSA2");
        alipayConfig.setEncryptor("");
        alipayConfig.setFormat("json");
        alipayConfig.setCertPath("C:/Users/AMD/Desktop/Alipaycertificate/apply/appPublicCert.crt");
        alipayConfig.setAlipayPublicCertPath("C:/Users/AMD/Desktop/Alipaycertificate/publickey/alipayPublicCert.crt");
        alipayConfig.setRootCertPath("C:/Users/AMD/Desktop/Alipaycertificate/base/alipayRootCert.crt");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayFundTransUniTransferRequest request = new AlipayFundTransUniTransferRequest();
        AlipayFundTransUniTransferModel model = new AlipayFundTransUniTransferModel();
        model.setOutBizNo("202306300001");
        model.setRemark("201905代发");
        model.setBusinessParams("{\"payer_show_name_use_alias\":\"true\"}");
        model.setBizScene("DIRECT_TRANSFER");
        Participant payeeInfo = new Participant();
        payeeInfo.setIdentity("2088722007622242");
        payeeInfo.setIdentityType("IDENTITY_CARD");
        payeeInfo.setName("aaa公司");
        model.setPayeeInfo(payeeInfo);
        model.setTransAmount("23.00");
        model.setProductCode("TRANS_ACCOUNT_NO_PWD");
        model.setOrderTitle("201905代发");
        request.setBizModel(model);
        AlipayFundTransUniTransferResponse response = alipayClient.certificateExecute(request);
        System.out.println(response.getBody());
        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
    }
}
